/**
 * @overview 创建贴地路宽
 * @author dengqiang
 * @description 鲸云CgsRect
 * @update  2022.03.26
 */
class GCgsRect {
  constructor() {
    window.GCgsRectArray = []
  }

  //新增单个贴地路宽
  add(paramObj) {
    const { name, parentName, option } = paramObj
    const materialUtilImage = new CgsMaterialUtil()
    //材质对象
    const imageMaterialPropertyImage = materialUtilImage.createMaterialProperty('Image', {
      image: option.imgUrl,
    })
    //贴地矩形
    const optRect1Image = {
      positions: [
        [option.pos.lng - 0.00006, option.pos.lat - 0.00005],
        [option.pos.lng + 0.00006, option.pos.lat + 0.00005],
      ],
      name: '',
      rotation: 0,
      material: imageMaterialPropertyImage,
      materialState: true,
      outline: false,
      labelfont: '20px 宋体',
      height: option.pos.height + 2, //不要给高度，默认贴在地形上,
      zIndex: 1000,
    }
    const GCgsRect = new CgsRect(optRect1Image)
    GCgsRect.feature3D.attribute = option.attribute //支持添加自定义属性

    const rectImageObject = {
      name: name,
      parentName: parentName,
      imageRect: GCgsRect,
    }
    window.GCgsRectArray.push(rectImageObject)
    window.map.addFeature(GCgsRect)
  }

  //根据name移除单个贴地路宽
  removeByName(name) {
    window.GCgsRectArray.forEach((item, index) => {
      if (item.name == name) {
        window.map.removeFeature(item.imageRect)
        window.GCgsRectArray.splice(index, 1)
      }
    })
  }

  //根据parentName移除一批贴地路宽
  removeByPatName(parentName) {
    const length = window.GCgsRectArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GCgsRectArray[i].parentName == parentName) {
        window.map.removeFeature(window.GCgsRectArray[i].imageRect)
        window.GCgsRectArray.splice(i, 1)
      }
    }
  }

  //移除所有贴地路宽
  removeAll() {
    window.GCgsRectArray.forEach((item) => {
      window.map.removeFeature(item.imageRect)
    })
    window.GCgsRectArray = []
  }

  //根据name控制单个贴地路宽的显隐
  setVisibleByName(name, boolean) {
    window.GCgsRectArray.forEach((item) => {
      if (item.name == name) {
        item.imageRect.setVisible(boolean)
      }
    })
  }

  //根据parentName控制一批贴地路宽的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GCgsRectArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.imageRect.setVisible(boolean)
      }
    })
  }
}

export default GCgsRect
